/**
 * 模板素材模块
 */
define( [
	'jquery',
	'require',
	'../../modules/createEditor',
	'api',
	'bigwe_utils',
	'message',
	'../../modules/templatePreview',
	'constants'
], function( $, require, editorObj, BIGWE_API, bu ) {

	var returnParam = {};

	var ShowTitleMessage = require( 'message' );
	var tp = require( '../../modules/templatePreview' );
	var c = require( 'constants' );
	var showTime = ShowTitleMessage.showTime;

	var tSelf = $( '.formwork-div' );
	var $templatePreviw = tp.$selector;
	// 模板数据初始化
	returnParam.templateInit = function() {
		if ( !template[ 0 ].isInit ) {
			template[ 0 ].ajaxRequest();
		}
	}

	var template = [ {
		param: {
			p: 1
		},
		ajaxRequest: function( option ) {
			this.isLoading = !0;
			$.get( BIGWE_API.SYSTEM_TEMPLATE.GET_SYSTEM_TEMPLATE, $.extend( {}, this.param, option ), $.proxy( function( data ) {
				this.isLoading = 0;
				this.isInit = !0;
				data = $.parseJSON( data );
				if ( !data.ret ) {
					for ( i in data.data.data ) {
						var temData = data.data.data[ i ];
						var $templateItem = $( this.htmlRander( temData ) ).data( temData );
						$( '.formwork-resource .spinner', tSelf ).before( $templateItem );
					}
					$( '.formwork-resource .js_template_prev' ).tooltip( {
						container: 'body'
					} );
				} else if ( data.ret == 80302 ) {
					this.isNodata = !0;
					$( '.formwork-resource .spinner', tSelf ).html( '没有更多数据了' );
				}
			}, this ) );
		},
		htmlRander: function( dataItem, tempType ) {
			var html = '';

			html += '<div class="fw-details" temp_type="' + ( tempType || 'system' ) + '" style="background-image: url(' + ( bu.wechartImgCheck( dataItem.pic ) || 'http://img01.bigwe.com/template_default.png' ) + ')">';
			html += '<div class="title_filter">';
			html += '<span class="text bw-ellipsis">' + ( dataItem.title || '' ) + '</span>';
			html += '<span class="iconfont icon-yulan pull-right js_template_prev" title="模板预览"></span>';
			html += '</div>';
			html += '</div>';

			return html;
		}
	}, {
		param: {
			p: 1
		},
		ajaxRequest: function( option ) {
			this.isLoading = !0;
			$.get( BIGWE_API.TEMPLATE.GET_TEMPLATE_LIST, $.extend( {}, this.param, option ), $.proxy( function( data ) {
				this.isLoading = 0;
				this.isInit = !0;
				data = $.parseJSON( data );
				if ( !data.ret ) {
					for ( i in data.data.data ) {
						var temData = data.data.data[ i ];
						var $templateItem = $( this.htmlRander( temData ) ).data( temData );
						$( '.formwork-own .spinner', tSelf ).before( $templateItem );
					}
					$( '.formwork-own .js_template_prev' ).tooltip( {
						container: 'body'
					} );
				} else if ( data.ret == 80306 ) {
					this.isNodata = !0;
					$( '.formwork-own .spinner', tSelf ).html( data.msg || '' );
				}
			}, this ) );
		},
		htmlRander: function( dataItem ) {
			var html = '';

			html += '<div class="fw-details" temp_type="personal" style="background-image: url(' + ( bu.wechartImgCheck( dataItem.pic ) || 'http://img01.bigwe.com/template_default.png' ) + ')">';
			html += '<div class="title_filter">';
			html += '<span class="text bw-ellipsis">' + ( dataItem.title || '' ) + '</span>';
			html += '<span class="iconfont icon-yulan pull-right js_template_prev" title="模板预览"></span>';
			html += '</div>';
			html += '</div>';
			return html;
		}
	} ];

	$( '.formwork-own', tSelf ).scrollMax( function() {
		var myTemplate = template[ 1 ];
		if ( !myTemplate.isLoading && !myTemplate.isNodata ) {
			myTemplate.ajaxRequest( {
				p: ++myTemplate.param.p
			} );
		}
	} );

	$( '.formwork-resource', tSelf ).scrollMax( function() {
		var myTemplate = template[ 0 ];
		if ( !myTemplate.isLoading && !myTemplate.isNodata ) {
			myTemplate.ajaxRequest( {
				p: ++myTemplate.param.p
			} );
		}
	} );

	function getTempContent( fn ) {
		var tempType = $( this ).attr( 'temp_type' );
		var tid = $( this ).data( 'tid' ) || $( this ).data( 'id' );
		var url = '';
		if ( tempType == 'system' ) {
			url = BIGWE_API.SYSTEM_TEMPLATE.GET_TEM_BYID + '?id=' + tid;
		} else if ( tempType == 'personal' ) {
			url = '/template/get_template?tid=' + tid;
		}

		return $.get( url, fn );
	}


	tSelf.on( 'click', '.title_filter .js_template_prev', function( e ) {
		e.stopPropagation();
		var tid = $( this ).parents( '.fw-details' ).data( 'tid' ) || $( this ).parents( '.fw-details' ).data( 'id' );
		var tempType = $( this ).parents( '.fw-details' ).attr( 'temp_type' );
		tp.slideIn();

		if ( $templatePreviw.data( 'tid' ) != tid || $templatePreviw.data( 'tempType' ) != tempType ) {
			tp.show( $( this ).parents( '.fw-details' ).data( 'title' ) );
			getTempContent.call( $( this ).parents( '.fw-details' ), function( data ) {

				data = $.parseJSON( data );
				if ( !data.ret ) {
					tp.contentRander( {
						'tid': tid,
						'tempType': tempType,
						'content': data.data.content
					} );
				}

			} );
		}
	} ).on( 'click', '.formworks-detail .fw-details', function() {
		ShowTitleMessage.show( '正在载入模板数据' );
		getTempContent.call( this, function( data ) {
			ShowTitleMessage.hide();
			data = $.parseJSON( data );
			if ( !data.ret ) {
				editorObj.execCommand( 'inserthtml', ( data.data.content || '' ).replace( /\/Public\/static/g, c.imgDomain[ 0 ] ).replace(/\r|\n/g, '') );
			}
		} );
	} );

	$( '.content-list>.formwork-div' ).show();
	$( '[event-columns="formwork"]' ).bwColumns( [ '素材模板', '我的模板', /*'我的收藏',*/ ], function( index ) {
		var index = index.index();
		var templateObj = template[ index ];
		$( '.formworks-detail>div' ).hide();
		$( '.formworks-detail>div' ).eq( index ).show();
		if ( !templateObj.isInit ) {
			templateObj.ajaxRequest();
		}

	} );
	$( '.content-list>.formwork-div' ).hide();


	returnParam.htmlBuild = template[ 0 ].htmlRander;

	return returnParam;
} );